package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TFileInfo;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TSystemRole;
import com.pubinfo.passbook.common.entity.TWiteoffInstance;
import com.pubinfo.passbook.common.mapper.TFileInfoMapper;
import com.pubinfo.passbook.common.mapper.TSourcePassbookMapper;
import com.pubinfo.passbook.common.mapper.TWiteoffInstanceMapper;
import com.pubinfo.passbook.common.model.bo.valueAddedTax.*;
import com.pubinfo.passbook.common.model.bo.witeoff.QueryWiteoffInstanceParam;
import com.pubinfo.passbook.common.model.dto.financial.CancelClaimFeed;
import com.pubinfo.passbook.common.model.dto.financial.ClaimRecord;
import com.pubinfo.passbook.common.model.dto.financial.FeedbackParam;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.http.param.TWiteoffInstanceReplay;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.service.master.*;
import com.pubinfo.passbook.common.service.mss.HttpService;
import com.pubinfo.passbook.common.service.request.financial.RequestFinancialServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TWiteoffInstanceServiceImpl extends ServiceImpl<TWiteoffInstanceMapper, TWiteoffInstance>
        implements TWiteoffInstanceService {

    @Autowired
    TPassbookService tPassbookService;
    @Autowired
    HttpService httpService;

    @Autowired
    RequestFinancialServer requestFinancialServer;
    @Autowired
    TSourcePassbookReimburseService reimburseService;
    @Autowired
    TRuleUserWiteoffService witeoffService;
    @Autowired
    ITFileElectronicFtpService tFileElectronicFtpService;
    @Autowired
    ITFileEnclosureFtpService tFileEnclosureFtpService;
    @Autowired
    TSourcePassbookMapper tSourcePassbookMapper;
    @Autowired
    TFileInfoMapper tFileInfoMapper;
    
    @Override
    public MyPage<TWiteoffInstance> QueryTwiteoffInstance(QueryWiteoffInstanceParam instanceParam, TSystemRole role, UserInfo userInfo) {
        
        MyPage<TWiteoffInstance> myPage = new MyPage<>(instanceParam);
        //普通用户  查询自己的，省公司报账员，查询他部门员工的报账单，医药费审核员查询他所在公司的报账单
        instanceParam.setRoleCode(role.getRoleCode());
        instanceParam.setAcct(userInfo.getJthrLoginName().split("@")[0]);
        Long totalPage = 0L;
        totalPage = this.baseMapper.QueryWiteoffInstanceListCount(instanceParam);
        List<TWiteoffInstance> list = this.baseMapper.QueryWiteoffInstanceList(instanceParam);
        if (totalPage == 0) {
            return myPage;
        }
        Long pageCount = (totalPage - 1) / instanceParam.getSize() + 1;
        myPage.setCountPage(pageCount);
        myPage.setTotalItem(totalPage);
        myPage.setData(list);
        return myPage;
        //  return this.baseMapper.QueryWiteoffInstanceList(iPage, instanceParam);
    }

    @Override
    public void cancelClaimFeedOrClaimFeedback() throws UnirestException {
//        List<TWiteoffInstance> tWiteoffInstances = this.baseMapper.selectCanCancelOrFeedback();
//        List<InputParamItem> inputParamItems = new ArrayList<>();
//        tWiteoffInstances.stream().forEach(tWiteoffInstance -> inputParamItems.add(new InputParamItem(tWiteoffInstance.getWiteoffInstanceCode())));
//        OutParam outParam = httpService.synWriteoffInfo(new InputParamBody(inputParamItems));
//        List<OutParamItem> outParamItems = outParam.getItems();
//        outParamItems.stream().forEach(outParamItem ->
//        {
//            if (outParamItem.getStatus() == 3) {
//                claimFeedback(outParamItem);
//                UpdateWrapper<TWiteoffInstance> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("witeoff_Instance_code", outParamItem.getWriteoffInstanceCode());
//                updateWrapper.set("is_cancel_or_feedback", 1);
//                update(updateWrapper);
//            }
//            else if(outParamItem.getStatus() == -3 || outParamItem.getStatus() == -2 || outParamItem.getStatus() == -1)
//            {
//                UpdateWrapper<TWiteoffInstance> updateWrapper = new UpdateWrapper<>();
//                updateWrapper.eq("witeoff_Instance_code", outParamItem.getWriteoffInstanceCode());
//                updateWrapper.set("is_cancel_or_feedback", 1);
//                update(updateWrapper);
//            }
//        });

    }

    @Override
    public TWiteoffInstance getTwiteoffInstanceById(String id, TSystemRole systemRole, String jthrLoginName) {
        TWiteoffInstance tWiteoffInstance = this.baseMapper.selectById(id);
    
        TWiteoffInstanceReplay tWiteoffInstanceReplay = new TWiteoffInstanceReplay(tWiteoffInstance);
        List<TFileInfo> tFileInfos = new ArrayList<>();
        tFileInfos.addAll(tFileEnclosureFtpService.selectEnclosureByInstanceId(tWiteoffInstanceReplay.getOtherSystemMainId()));
        tFileInfos.addAll(tFileElectronicFtpService.selectElectronicByInstanceId(id));
        tFileInfos.addAll(tFileInfoMapper.selectArtificialFIleById(tWiteoffInstance.getOtherSystemMainId()));
        tWiteoffInstanceReplay.setTFileInfos(tFileInfos);
        tWiteoffInstanceReplay.setTPassbooks(tSourcePassbookMapper.findPassbookByWiteoffId(id));
        return tWiteoffInstanceReplay;
    }

    @Override
    public TWiteoffInstance getTwiteoffInstanceByOtherSystemMainId(String otherSystemMainId) {
        return this.baseMapper.selectTwiteoffInstanceByOtherSystemMainId(otherSystemMainId);
    }


    @Override
    //撤销
    public void cancelClaimFeed(OutParamItem outParamItem) {
        CancelClaimFeed cancelClaimFeed = new CancelClaimFeed();
        cancelClaimFeed.setAccNumber(outParamItem.getWriteoffInstanceCode());
        cancelClaimFeed.setAccDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        requestFinancialServer.cancelClaimFeed(cancelClaimFeed);
    }

    //回传
    @Override
    public void claimFeedback(OutParamItem outParamItem) {
        List<TPassbook> tPassbooks = new ArrayList<>();
        for (OutParamInvoiceInfo outParamInvoiceInfo : outParamItem.getInvoiceInfos()) {
            TPassbook tPassbook = new TPassbook();
            tPassbook.setEinvoiceCode(outParamInvoiceInfo.getInvoiceCode());
            tPassbook.setEinvoiceNumber(outParamInvoiceInfo.getInvoiceNum());
            tPassbooks.add(tPassbook);
        }
        tPassbooks = tPassbookService.selectPassbookByFpdmAndFphm(tPassbooks);
        List<ClaimRecord> claimRecords = new ArrayList<>();
        for (TPassbook tPassbook : tPassbooks) {
            ClaimRecord claimRecord = new ClaimRecord();
            claimRecord.setEInvoiceId(tPassbook.getEInvoiceId());
            claimRecord.setClaimAmount(tPassbook.getReimbursableSum().toString());
            claimRecords.add(claimRecord);
        }
        FeedbackParam feedbackParam = new FeedbackParam();
        feedbackParam.setAccNumber(outParamItem.getWriteoffInstanceCode());
        feedbackParam.setAccDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        feedbackParam.setClaimRecords(claimRecords);
        requestFinancialServer.claimFeedback(feedbackParam);
    }
}
